package praca.framework;

import android.util.FloatMath;

public class Vector {

	public float x;
	public float y;
	
	public static float TO_RADIANS = (1 / 180.0f) * (float) Math.PI;
	public static float TO_DEGREES = (1 / (float) Math.PI) * 180;
	
	public Vector(float x, float y){
		this.x = x;
		this.y = y;
	}
	
	public Vector(Vector vector){
		this.x = vector.x;
		this.y = vector.y;
	}
	
	public Vector copy(){
		return new Vector(x,y);
	}
	
	public Vector setXY(float x, float y){
		this.x = x;
		this.y = y;
		return this;
	}
	
	public Vector add(float x, float y){
		this.x += x;
		this.y += y;
		
		return this;
	}
	
	public Vector sub(float x, float y){
		this.x -= x;
		this.y -= y;
		
		return this;
	}
	
	public Vector sub(Vector v){
		this.x -= v.x;
		this.y -= v.y;
		
		return this;
	}
	
	public Vector mul(float scalar){
		this.x *= scalar;
		this.y *= scalar;
		
		return this;
	}
	
	public float len() {
		return FloatMath.sqrt(x * x + y * y);
	}
	
	public Vector normalize(){
		float len = this.len();
		if(len!= 0){
			this.x /= len;
			this.y /= len;
		}
		
		return this;
	}
	
	public float angle() {
		float angle = (float) Math.atan2(y, x) * TO_DEGREES;
	//	Log.d("touch", "x: "+x+", y:"+y+" atan="+(Math.atan2(y, x) * TO_DEGREES));
		if (angle < 0)
			angle += 360;
		return angle;
	}
	
	public Vector rotate(float angle) {
		float rad = angle * TO_RADIANS;
		
		float cos = FloatMath.cos(rad);
		float sin = FloatMath.sin(rad);
		
		float newX = this.x * cos - this.y * sin;
		float newY = this.x * sin + this.y * cos;
		
		this.x = newX;
		this.y = newY;
		
		return this;
	}
	
	public float dist(Vector other) {
		float distX = this.x - other.x;
		float distY = this.y - other.y;
		
		return (float) Math.sqrt(distX * distX + distY * distY);
		
	}
	
	public float dist(float x, float y) {
		float distX = this.x - x;
		float distY = this.y - y;
		
		return (float) Math.sqrt(distX * distX + distY * distY);
	}
	
	public float distSquared(Vector other) {
		float distX = this.x - other.x;
		float distY = this.y - other.y;
		return distX * distX + distY * distY;
	}
	
	public float distSquared(float x, float y) {
		float distX = this.x - x;
		float distY = this.y - y;
		return distX * distX + distY * distY;
	}
}
